package com.hmall.order.web;

import com.hmall.common.dto.client.ItemClient;
import com.hmall.common.dto.client.UserClient;
import com.hmall.common.dto.pojo.Address;
import com.hmall.common.dto.pojo.Item;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderParms;
import com.hmall.order.pojo.TbOrderDetail;
import com.hmall.order.pojo.TbOrderLogistics;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {
   @Autowired
   ItemClient itemClient;

   @Autowired
   UserClient userClient;

   @Autowired
   private IOrderService orderService;

   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   @PostMapping
   public Long order(@RequestBody OrderParms orderParms){

      Item item1 = itemClient.findItemById(Long.valueOf(orderParms.getItemId()));

      Long price1 = item1.getPrice();

      Order order = new Order();

      order.setTotalFee(price1*(Long.valueOf(orderParms.getNum())));
      order.setPaymentType(3);
      order.setStatus(1);


      //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
      Long id = order.getId();
      TbOrderDetail orderDetail = new TbOrderDetail();
      orderDetail.setOrderId(id);

      Long id1 = item1.getId();
      orderDetail.setItemId(id1);

      orderDetail.setNum(Integer.valueOf(orderParms.getNum()));
      orderDetail.setName(item1.getName());
      orderDetail.setPrice(item1.getPrice());


      Address address = userClient.findAddressById(Long.valueOf(orderParms.getItemId()));

      //log
      TbOrderLogistics tb = new TbOrderLogistics();

      tb.setContact(address.getContact());
      tb.setMobile(address.getMobile());
      tb.setProvince(address.getProvince());
      tb.setProvince(address.getCity());
      tb.setTown(address.getTown());
      tb.setStreet(address.getStreet());
      tb.setCity(address.getCity());

      Long saves = orderService.saves(order, orderDetail, tb);

//      itemClient.test(itemById.getId(),Integer.parseInt(orderParms.getNum()));
      return saves;
   }
}
